package simple;

import com.rabbitmq.client.*;
import org.junit.Test;
import util.ConnUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author TZL
 * @date 2020/8/13
 * 消费者
 */
public class Consumer {
    @Test
    public void consume() throws IOException, TimeoutException, TimeoutException {
        //1. 获取连接对象
        Connection conn = ConnUtil.getConn();

        //2. 创建channel
        Channel channel = conn.createChannel();

        //3. 声明队列
        channel.queueDeclare(Publisher.QUEUE_NAME,true,false,false,null);

        //4. 消费当前队列
        DefaultConsumer callback = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("获取到消息:" + new String(body,"UTF-8"));
            }
        };
        channel.basicConsume(Publisher.QUEUE_NAME,true,callback);

        System.in.read();

        //5. 释放资源
        channel.close();
        conn.close();
    }
}
